package net.mingsoft.cmsMessage.action;

import java.util.List;
import java.io.File;
import java.util.ArrayList;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.hutool.core.util.ObjectUtil;
import java.util.*;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.cmsMessage.dto.ResoutDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.BindingResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.ui.ModelMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.stream.Collectors;

import org.springframework.web.bind.annotation.*;
import net.mingsoft.cmsMessage.biz.ICmsMessageBiz;
import net.mingsoft.cmsMessage.entity.CmsMessageEntity;
import net.mingsoft.base.util.JSONObject;
import net.mingsoft.base.entity.BaseEntity;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.basic.util.StringUtil;
import net.mingsoft.base.filter.DateValueFilter;
import net.mingsoft.base.filter.DoubleValueFilter;
import net.mingsoft.basic.bean.EUListBean;
import net.mingsoft.basic.annotation.LogAnn;
import net.mingsoft.basic.constant.e.BusinessTypeEnum;
import org.apache.commons.lang3.StringUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
/**
 * 消息推送管理控制层
 * @author ym
 * 创建日期：2022-1-2 16:29:22<br/>
 * 历史修订：<br/>
 */
@Api(value = "消息推送接口")
@Controller("cmsmessageCmsMessageAction")
@RequestMapping("/${ms.manager.path}/cmsMessage/cmsMessage")
public class CmsMessageAction extends BaseAction{


	/**
	 * 注入消息推送业务层
	 */
	@Autowired
	private ICmsMessageBiz cmsMessageBiz;

	/**
	 * 返回主界面index
	 */
	@GetMapping("/index")
	public String index(HttpServletResponse response,HttpServletRequest request) {
		return "/cmsMessage/cms-message/index";
	}

	/**
	 * 查询消息推送列表
	 * @param cmsMessage 消息推送实体
	 */
	@ApiOperation(value = "查询消息推送列表接口")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "messageStatus", value = "消息状态", paramType = "query"),
			@ApiImplicitParam(name = "pushPerson", value = "推送目标", paramType = "query"),
			@ApiImplicitParam(name = "pushTime", value = "推送时间", paramType = "query"),
			@ApiImplicitParam(name = "content", value = "内容", paramType = "query"),
			@ApiImplicitParam(name = "tittle", value = "标题", paramType = "query"),
    })
	@RequestMapping(value ="/list",method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public ResultData list(@ModelAttribute @ApiIgnore CmsMessageEntity cmsMessage,HttpServletResponse response, HttpServletRequest request,@ApiIgnore ModelMap model,BindingResult result) {
		BasicUtil.startPage();
		List cmsMessageList = null;
		if ( cmsMessage.getSqlWhere() != null){
			cmsMessageList = cmsMessageBiz.query(cmsMessage);
		} else {
			QueryWrapper queryWrapper = new QueryWrapper<CmsMessageEntity>();
			if(StrUtil.isNotEmpty(cmsMessage.getTittle())){
				queryWrapper.like("TITTLE",cmsMessage.getTittle().trim());
			}
			cmsMessageList = cmsMessageBiz.list(queryWrapper);
		}
		return ResultData.build().success(new EUListBean(cmsMessageList,(int)BasicUtil.endPage(cmsMessageList).getTotal()));
	}

	/**
	 * 查询消息可推送人员列表
	 */
	@ApiOperation(value = "查询消息可推送人员列表接口")
	@RequestMapping(value ="/personlist",method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public ResultData personList(HttpServletResponse response,
								 HttpServletRequest request,
								 @ApiIgnore ModelMap model,BindingResult result) {
		ResultData resultData = new ResultData();
		List<ResoutDto> personList = cmsMessageBiz.queryAllPerson();
		resultData.isSuccess();
		resultData.success(personList);
		return resultData;
	}

	/**
	 * 返回编辑界面cmsMessage的form
	 */
	@GetMapping("/form")
	public String form(@ModelAttribute CmsMessageEntity cmsMessage,HttpServletResponse response,HttpServletRequest request,ModelMap model) {
		return "/cmsMessage/cms-message/form";
	}


	/**
	 * 获取消息推送
	 * @param cmsMessage 消息推送实体
	 */
	@ApiOperation(value = "获取消息推送列表接口")
    @ApiImplicitParam(name = "id", value = "主键ID", required =true,paramType="query")
	@GetMapping("/get")
	@ResponseBody
	public ResultData get(@ModelAttribute @ApiIgnore CmsMessageEntity cmsMessage,HttpServletResponse response, HttpServletRequest request,@ApiIgnore ModelMap model) {
		if (cmsMessage.getId()==null) {
			return ResultData.build().error();
		}
		CmsMessageEntity _cmsMessage = (CmsMessageEntity)cmsMessageBiz.getById(cmsMessage.getId());
		return ResultData.build().success(_cmsMessage);
	}


	/**
	* 保存消息推送
	* @param cmsMessage 消息推送实体
	*/
	@ApiOperation(value = "保存消息推送列表接口")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "messageStatus", value = "消息状态", required =true, paramType = "query"),
			@ApiImplicitParam(name = "pushPerson", value = "推送目标", required =true, paramType = "query"),
			@ApiImplicitParam(name = "pushTime", value = "推送时间", required =true, paramType = "query"),
			@ApiImplicitParam(name = "content", value = "内容", required =true, paramType = "query"),
			@ApiImplicitParam(name = "tittle", value = "标题", required =true, paramType = "query"),
	})
	@PostMapping("/save")
	@ResponseBody
	@LogAnn(title = "保存消息推送", businessType = BusinessTypeEnum.INSERT)
	@RequiresPermissions("cmsMessage:cmsMessage:save")
	public ResultData save(@ModelAttribute @ApiIgnore CmsMessageEntity cmsMessage, HttpServletResponse response, HttpServletRequest request) {
		//验证推送目标的值是否合法
		if (StringUtils.isBlank(cmsMessage.getPushPerson())) {
			return ResultData.build().error(getResString("err.empty", this.getResString("push.person")));
		}
		//验证推送时间的值是否合法
		if (cmsMessage.getPushTime()==null) {
			return ResultData.build().error(getResString("err.empty", this.getResString("push.time")));
		}
		//验证内容的值是否合法
		if (StringUtils.isBlank(cmsMessage.getContent())) {
			return ResultData.build().error(getResString("err.empty", this.getResString("content")));
		}
		//验证标题的值是否合法
		if (StringUtils.isBlank(cmsMessage.getTittle())) {
			return ResultData.build().error(getResString("err.empty", this.getResString("tittle")));
		}
		if (!StringUtil.checkLength(cmsMessage.getTittle()+"", 0, 255)) {
			return ResultData.build().error(getResString("err.length", this.getResString("tittle"), "0", "255"));
		}
		cmsMessageBiz.save(cmsMessage);
		return ResultData.build().success(cmsMessage);
	}

	/**
     *  删除消息推送
     *
	 * @param cmsMessages 消息推送实体
	 */
	@ApiOperation(value = "批量删除消息推送列表接口")
	@PostMapping("/delete")
	@ResponseBody
	@LogAnn(title = "删除消息推送", businessType = BusinessTypeEnum.DELETE)
	@RequiresPermissions("cmsMessage:cmsMessage:del")
	public ResultData delete(@RequestBody List<CmsMessageEntity> cmsMessages,HttpServletResponse response, HttpServletRequest request) {
	    List<String> ids = (List)cmsMessages.stream().map((p) -> {return p.getId();}).collect(Collectors.toList());
		return this.cmsMessageBiz.removeByIds(ids) ? ResultData.build().success() : ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("id")}));
		}

	/**
	 *	更新消息推送列表
	 *
	 * @param cmsMessage 消息推送实体
	 */
	 @ApiOperation(value = "更新消息推送列表接口")
	 @ApiImplicitParams({
    		@ApiImplicitParam(name = "messageStatus", value = "消息状态", required =true, paramType = "query"),
    		@ApiImplicitParam(name = "pushPerson", value = "推送目标", required =true, paramType = "query"),
    		@ApiImplicitParam(name = "pushTime", value = "推送时间", required =true, paramType = "query"),
    		@ApiImplicitParam(name = "content", value = "内容", required =true, paramType = "query"),
    		@ApiImplicitParam(name = "tittle", value = "标题", required =true, paramType = "query"),
	})
	@PostMapping("/update")
	@ResponseBody
	@LogAnn(title = "更新消息推送", businessType = BusinessTypeEnum.UPDATE)
	@RequiresPermissions("cmsMessage:cmsMessage:update")
	public ResultData update(@ModelAttribute @ApiIgnore CmsMessageEntity cmsMessage, HttpServletResponse response,
			HttpServletRequest request) {
		//验证推送目标的值是否合法
		if (StringUtils.isBlank(cmsMessage.getPushPerson())) {
			return ResultData.build().error(getResString("err.empty", this.getResString("push.person")));
		}
		//验证推送时间的值是否合法
		if (cmsMessage.getPushTime()==null) {
			return ResultData.build().error(getResString("err.empty", this.getResString("push.time")));
		}
		//验证内容的值是否合法
		if (StringUtils.isBlank(cmsMessage.getContent())) {
			return ResultData.build().error(getResString("err.empty", this.getResString("content")));
		}
		//验证标题的值是否合法
		if (StringUtils.isBlank(cmsMessage.getTittle())) {
			return ResultData.build().error(getResString("err.empty", this.getResString("tittle")));
		}
		if (!StringUtil.checkLength(cmsMessage.getTittle()+"", 0, 255)) {
			return ResultData.build().error(getResString("err.length", this.getResString("tittle"), "0", "255"));
		}
			cmsMessageBiz.updateById(cmsMessage);
		return ResultData.build().success(cmsMessage);
	}


}
